﻿@using System.Reflection
@using XCode
@using XCode.Configuration
@{
    Layout = null;

    var role = Model.Role as Role;
    var menus = Menu.Root.AllChilds;
    //var pfs = EnumHelper.GetDescriptions<PermissionFlags>().Where(e => e.Key > PermissionFlags.None);
}
<script type="text/javascript">
    $(function () {
        // 授权列
        $('input.authorize').on('change', function () {
            var $this = $(this);
            var status = $this.prop('checked');
            var childkey = $this.attr('child');
            // 规避change 需要在失去焦点时触发的问题，设置值完成后手工再次触发该事件
            $('input[parentkey="' + childkey + '"]').prop('checked', status);
        });

        // 只读/查看
        $('input.pro_detail').on('change', function () {
            $this = $(this);
            var status = $this.prop('checked');
            var key = $this.attr('prochildkey');
            $('input[proparentkey=' + key + ']').prop('checked', status);
        });
        // 全部权限
        $('input.pro_all').on('change', function () {
            $this = $(this);
            var status = $this.prop('checked');
            var key = $this.attr('prochildkey');
            $('input[prokey=' + key + ']').prop('checked', status);
        });
    });
</script>
<div class="form-group col-md-12">
    <label class="control-label col-md-2">授权</label>
    <div class="input-group col-md-10">
        <table class="table table-bordered table-hover table-striped table-condensed">
            <thead>
                <tr>
                    <th>名称</th>
                    <th>显示名</th>
                    <th>授权</th>
                    <th>操作权限</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var entity in menus)
                {
                    <tr>
                        <td>@entity.TreeNodeName</td>
                        <td>@entity.DisplayName</td>
                        <td>@Html.CheckBox("p" + entity.ID, role.Has(entity.ID), new { @class = "authorize", @child = "auth_child" + entity.ID, @parentkey = "auth_child" + entity.ParentID })</td>
                        <td>
                            @if (entity.Childs.Count == 0)
                            {
                                foreach (var item in entity.Permissions.OrderBy(e => e.Key))
                                {
                                    var id = "pf" + entity.ID + "_" + ((Int32)item.Key);
                                    @Html.CheckBox(id, role.Has(entity.ID, (PermissionFlags)item.Key), new { @parentkey = "auth_child" + entity.ID, @proparentkey = "pro_" + item.Key + "_" + entity.ParentID, @prokey = "pro_" + (Int32)PermissionFlags.All + "_" + entity.ParentID })
                                    @Html.Label(id, item.Value)
                                    <text>&nbsp;</text>
                                }
                            }
                            else
                            {

                                @Html.CheckBox("pc_readonly_" + entity.ID, false, new { @class = "pro_detail", @prochildkey = "pro_" + (Int32)PermissionFlags.Detail + "_" + entity.ID })
                                @Html.Label("pc_readonly_" + entity.ID, "只读")
                                <text>&nbsp;</text>
                                @Html.CheckBox("pc_all_" + entity.ID, false, new { @class = "pro_all", @prochildkey = "pro_" + (Int32)PermissionFlags.All + "_" + entity.ID })
                                @Html.Label("pc_all_" + entity.ID, "全部")
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>